package xyz.haijin.flow.liteflow.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.haijin.flow.liteflow.dto.VueFlowRequest;
import xyz.haijin.flow.liteflow.service.VueFlowParserService;

import java.util.HashMap;
import java.util.Map;

/**
 * Vue Flow控制器
 * 提供解析Vue Flow流程图的API接口
 * 
 * @author haijin
 * @since 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/api/vueflow")
public class VueFlowController {

    @Autowired
    private VueFlowParserService vueFlowParserService;

    /**
     * 解析Vue Flow流程图，生成LiteFlow EL表达式
     * 
     * @param vueFlowRequest Vue Flow请求数据
     * @return 解析结果
     */
    @PostMapping("/parse")
    public Map<String, Object> parseVueFlow(@RequestBody VueFlowRequest vueFlowRequest) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            log.info("收到Vue Flow解析请求，节点数量: {}", 
                    vueFlowRequest.getNodes() != null ? vueFlowRequest.getNodes().size() : 0);
            
            // 解析Vue Flow流程图
            String elExpression = vueFlowParserService.parseToElExpression(vueFlowRequest);
            
            response.put("success", true);
            response.put("message", "Vue Flow流程图解析成功");
            response.put("elExpression", elExpression);
            
            log.info("Vue Flow流程图解析成功，生成的EL表达式: {}", elExpression);
            
        } catch (Exception e) {
            log.error("Vue Flow流程图解析失败", e);
            
            response.put("success", false);
            response.put("message", "Vue Flow流程图解析失败");
            response.put("error", e.getMessage());
        }
        
        return response;
    }

    /**
     * 健康检查接口
     * 
     * @return 健康状态
     */
    @GetMapping("/health")
    public Map<String, Object> health() {
        Map<String, Object> response = new HashMap<>();
        response.put("status", "UP");
        response.put("service", "Vue Flow Parser Service");
        response.put("timestamp", System.currentTimeMillis());
        return response;
    }
}
